﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonLibrary;
using DBO.Utility;
using Serveur.DataAccess.EF;

namespace Serveur.DataAccess
{
    public class SessionDataAccess : ISessionDataAccess
    {
        public string TryLogin(PLICCredentials Creds)
        {
            bool res = (from acc in ContextHandler.Context.AccountJeu
                        where acc.Login == Creds.Login && acc.Password == Creds.Password
                        select acc).Any();
            return res ? "ok" : null;
        }

        public bool CreateUser(PLICCredentials Creds, System.Net.Mail.MailAddress Address, string Race)
        {
            Race = Race.ToLower();
            Logger.ConsoleLogger.Log("SessionDA, create account for " + Address.Address);

            bool exist = (from acc in ContextHandler.Context.AccountJeu
                          where acc.Login == Creds.Login || acc.EMail == Address.Address
                          select acc).Any();
            if (exist)
            {
                Logger.ConsoleLogger.LogAnomaly("SessionDA, account for " + Address.Address + " already existed.");
                return false;
            }

            EF.Race Rc = null;
            try
            {
                Rc = (from race in ContextHandler.Context.RaceJeu
                      where race.Name.ToLower() == Race.ToLower()
                      select race).First();

            }
            catch (Exception)
            {
                Logger.ConsoleLogger.LogError("SessionDA, race not found : " + Race + ". Have you populated the DB ?");
                return false;
            }
            EF.Resources InitResources = new Resources()
            {
                Food = 10,
                Gold = 100,
                Stone = 150,
                Wood = 200
            };

            EF.Army Army = new Army();
            foreach (int item in ArmyConstants.PointValues)
            {
                Army.Squad.Add(new Squad()
                {
                    Army = Army,
                    PointValue = item
                });
            }
            EF.City City = new EF.City()
            {
                Name = Creds.Login + "'s city",
                Resources = InitResources
            };
            City.Resources = InitResources;
            UnitSlot slot1 = new UnitSlot() { Unit = Race == "heirs" ? t("footman") : t("militian"), CityID = City.ID };
            slot1.Number = 2;
            UnitSlot slot2 = new UnitSlot() { Unit = Race == "heirs" ? t("archer") : t("knife_thrower"), CityID = City.ID };
            slot2.Number = 1;
            UnitSlot slot3 = new UnitSlot() { Unit = Race == "heirs" ? t("knight") : t("ranger"), CityID = City.ID };

            City.UnitSlot.Add(slot1);
            City.UnitSlot.Add(slot2);
            City.UnitSlot.Add(slot3);
            
            EF.Player Player = new EF.Player()
            {
                Army = Army,
                City = City,
                Race = Rc
            };
            Army.Player = Player;
            City.Player = Player;

            EF.Account Account = new EF.Account()
            {
                EMail = Address.Address,
                Login = Creds.Login,
                Password = Creds.Password,
                Player = Player
            };

            Player.Account = Account;
            Account.Player = Player;

            ContextHandler.Context.PlayerJeu.AddObject(Player);

            ContextHandler.Context.SaveChanges();

            Logger.ConsoleLogger.Log("SessionDA, account for " + Address.Address + " created.");

            return true;
        }

        public string ChangePassword(string Login, System.Net.Mail.MailAddress Address)
        {
            string pass = Guid.NewGuid().ToString().Substring(0, 8);
            EF.Account Acc = (from acc
                              in ContextHandler.Context.AccountJeu
                              where acc.Login == Login
                              select acc).First();
            Acc.Password = pass;
            ContextHandler.Context.SaveChanges();
            return pass;
        }

        private UnitType t(string type)
        {
            return (from t in ContextHandler.Context.UnitTypeJeu
                    where t.Name.ToLower() == type.ToLower()
                    select t).First();

        }
    }
}
